package com.aidong.web.controller.card;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.MemberCardDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.model.wills.MemberCard;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"cardReceive"})
public class MemberCardReceiveController extends BaseController {
    @Resource
    MemberCardDao memberCardDao;
    @Resource
    MemberInfoDao memberInfoDao;

    public MemberCardReceiveController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("card/receive/list")).addObject("shopName", this.getTypeList("shopName")).addObject("cardStatus", this.getTypeList("cardStatus"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.in("c.status", new Object[]{Integer.valueOf(3), Integer.valueOf(4)});
        if(!this.chkNull(dateFrom)) {
            cd.ge("k.saleDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("k.saleDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.memberCardDao.queryPageSaleList(pg, cd);
        ModelAndView mv = new ModelAndView("card/receive/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"_recv"})
    @ResponseBody
    public String _recv(@RequestParam("cardCode") String cardCode) {
        MemberCard card = this.memberCardDao.getById(cardCode);
        if(card == null) {
            return this.getResult(false, "没有这个卡号");
        } else if(card.getStatus().intValue() < 3) {
            return this.getResult(false, "还未制卡,不能领卡");
        } else {
            card.setStatus(Integer.valueOf(5));
            card.setMemberReceiveUser(this.getLoginUserCode());
            card.setMemberReceiveDate(new Date());

            try {
                this.memberCardDao.updateMemberReceive(card);
                this.addUserLog("会员领卡", "卡号:" + card.getCardCode() + ",合同号:" + card.getContractCode() + ",会员号:" + card.getMemberCode());
            } catch (Exception var4) {
                this.log.error("card memberreceive", var4);
                return this.getResult(false, "会员领卡发生错误");
            }

            return this.getResult(true, "操作成功");
        }
    }
}